package be.nepherte.movmanager.client.gui.components;

import java.util.ArrayList;
import java.util.List;

/*
 * RowData defines one row in a Sortable Table
 */
@SuppressWarnings("unchecked")
public class RowData implements Comparable {
	/**The list of the column values in the table.*/
	List<Comparable<?>> columnValues = new ArrayList<Comparable<?>>();
	/**The current column index being sorted.*/
	int sortColIndex = 0;

	/**
	 * Add the comparable value to the list of column values of this row.
	 * 
	 * @param 	value
	 * 			the comparable item you want to add
	 */
	public void addColumnValue(Comparable<?> value){
		this.columnValues.add(value);
	}
	
	/**
	 * Add the comparable value to the list of column values of this row at the specified index.
	 * 
	 * @param	index
	 * 			the index of the column items where the value should be put in
	 * @param	value
	 * 			the comparable item you want to add
	 */
	public void addColumnValue(int index, Comparable<?> value){
		if (index >= this.columnValues.size())
			addNullColumns(index);
		
		this.columnValues.set(index, value);
	}	

	/**
	 * Get the comparable object from the column values of this row.
	 * 
	 * @param	index
	 * 			the index of the item you want to retrieve
	 * @return 	the object at the specified index in the column values list
	 */
	public Object getColumnValue(int index){
		return this.columnValues.get(index);
	}	
	
	/**
	 * Get the list of column values of this row.
	 * 
	 * @return	column values
	 */
	public List<Comparable<?>> getColumnValues() {
		return columnValues;
	}

	/**
	 * Set the list of comparable column values of this row.
	 * 
	 * @param	columnValues
	 * 			the list of comparable column values you want to set for this row
	 */
	public void setColumnValues(List<Comparable<?>> columnValues) {
		this.columnValues = columnValues;
	}

	/**
	 * Get the current column index being sorted.
	 * 
	 * @return 	sortColIndex
	 */
	public int getSortColIndex() {
		return sortColIndex;
	}

	/**
	 * Set the current column index that needs to be sorted.
	 * 
	 * @param	sortColIndex
	 * 			the current column index that needs to be sorted
	 */
	public void setSortColIndex(int sortColIndex) {
		this.sortColIndex = sortColIndex;
	}

	/**
	 * Implementation of the comparable interface: compare a column value of this row with the value in the same column of another row.
	 * 
	 * @param	other
	 * 			the other row that will be compared with this row
	 */
	public int compareTo(Object other) {
		if (null == other)
			return -1;
		
		RowData otherRow = (RowData)other;
		Comparable<Comparable<?>> obj1 = (Comparable<Comparable<?>>)this.getColumnValue(this.sortColIndex);
		Comparable<?> obj2 = (Comparable<?>)otherRow.getColumnValue(this.sortColIndex);
		return obj1.compareTo(obj2);
	}
	
	/**
	 * Add extra columns filled with null objects to the end of this row till you have reached index+1.
	 * If index is already an existing column, no columns will be added.
	 *  
	 * @param	index
	 * 			the index of the last extra column that has to be added and filled with null objects
	 */
	private void addNullColumns(int index){
		for (int nullIndex=this.columnValues.size(); nullIndex<=index; nullIndex++)
			columnValues.add(null);
	}
}
